package com.focusai.efairy.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.focusai.efairy.R;
import com.focusai.efairy.ui.adapter.SelectImgGirdAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luo_guangwen on 2017/10/13.
 */

public class InputGirdImageView extends RelativeLayout {
    private EditText editText;
    private RecyclerView mRecyclerView;
    private SelectImgGirdAdapter mAdapter;
    private List<String> mList;
    public static final String LAST = "last";
    public int DEFAUTECOUNT = 9;
    public int SpanCount = 4;

    public InputGirdImageView(Context context) {
        super(context);
//        init();
    }

    public InputGirdImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public InputGirdImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.gird_input_view);
        if (typedArray != null) {
            DEFAUTECOUNT = typedArray.getInt(R.styleable.gird_input_view_max_count, DEFAUTECOUNT);
            SpanCount = typedArray.getInt(R.styleable.gird_input_view_span_count, SpanCount);
            typedArray.recycle();
        }

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.view_input_image, this);
        editText = (EditText) view.findViewById(R.id.input_edit);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rcyc_list);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), SpanCount));
        mList = new ArrayList<>();
        mList.add(LAST);
        mAdapter = new SelectImgGirdAdapter(getContext());
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.refreshData(mList);
    }

    public void setOnItemClickListener(BaseRecyclerAdapter.OnItemClickListener onItemClickListener) {
        mAdapter.setOnItemClickListener(onItemClickListener);
    }

    public String getItem(BaseRecyclerViewHolder holder) {
        int position = mRecyclerView.getChildLayoutPosition(holder.itemView);
        return mAdapter.getData().get(position);
    }

    public int getPosition(BaseRecyclerViewHolder holder) {
        return mRecyclerView.getChildLayoutPosition(holder.itemView);
    }

    public String getContentText() {
        return this.editText.getText().toString();
    }

    public void refreshData(List<String> list) {
        this.mList.clear();
        this.mList.addAll(list);
        if (this.mList.size() < DEFAUTECOUNT) {
            this.mList.add("last");
        }
        mAdapter.notifyDataSetChanged();
    }

    public void refreshDataNoAdd(List<String> list) {
        this.mList.clear();
        this.mList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    public EditText getEditText() {
        return editText;
    }

    public void setEditText(EditText editText) {
        this.editText = editText;
    }

    public List<String> getmList() {
        return mList;
    }
}
